#ifndef DIALOGTESTEVENTCAPTURE_H
#define DIALOGTESTEVENTCAPTURE_H

#include <QTime>
#include <QDialog>
#include <QTimer>
#include<QDir>
#include <QFile>
#include<opencv2/opencv.hpp>
#include<time.h>
#include <iostream>
#include <fstream>
#include <qtconcurrentrun.h>
#include <QThread>
#include <QMutex>



class writer: public QObject {
Q_OBJECT
public:
    writer(cv::VideoCapture *cV, cv::VideoWriter *wV, QMutex *mut);
public slots:
    void writeFrame();
    void Close();
private:
    cv::VideoCapture *capVid;
    cv::VideoWriter *writeVid;
    cv::Mat matCap;
    QMutex *m;
};

namespace Ui {
class DialogTestEventCapture;
}

class DialogTestEventCapture : public QDialog
{
    Q_OBJECT
    
public:
    explicit DialogTestEventCapture(QWidget *parent = 0);
    ~DialogTestEventCapture();

    void init(QString eventName);

    int resWidth;
    int resHeight;

signals:
    void write0();
    void write1();
    void close0();
    void close1();
    
private slots:
    void on_spinBox_valueChanged(int arg1);

    void on_pushButton_Run_clicked();

    void TimerEvent();

    void fps();

    void on_checkBox_clicked(bool checked);

    void on_checkBox_ShowCams_clicked(bool checked);

private:
    Ui::DialogTestEventCapture *ui;

    int eventTime;    //millisecond
    int currentTime;  //millisecond
    QTimer* timer;
    QDir dirVideo;  //directory for the video files

    int recording;               // 1: recording, 0: not recording
    cv::VideoCapture capWebcam0;  //Webcam Capture, camera 1
    cv::VideoCapture capWebcam1;  //Webcam Capture, camera 2
    cv::VideoWriter record;      //Video Recorder, camera 1
    cv::VideoWriter record_2;      //Video Recorder, camera 2

    QString filename;           // Name of the AVI file to save, camera 1
    QString filename_2;         // Name of the AVI file to save, camera 2

    int fpsUpdateCounter;             // Timetracking
    int frameMax;            // interval of time track
    long int time, lasttime;    // time variables
    float timesecs;             // float value in seconds


    cv::Mat matWebcam;           //Frame Captured From Webcam 1
    cv::Mat matWebcam_2;           //Frame Captured From Webcam 2

    int frameCounter;
    QTime startTime;

    bool showCams;  //toggle to show cameras before capture starts.

    std::vector<int> timecode;

    QFuture<void> cam0Future,cam1Future;

    QMutex m0,m1;
    QThread *thread0,*thread1;
    writer *writer0,*writer1;
    
    int grabTime0, grabTimeLast0, grabTime1, grabTimeLast1,
        minLatency0, maxLatency0, minLatency1, maxLatency1,
        minLatency0to1, maxLatency0to1;
};

#endif // DIALOGTESTEVENTCAPTURE_H
